﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Net.Configuration;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Linq;

public partial class Clients_Control_lienhe : System.Web.UI.UserControl
{
    protected String returnValue;
    private Random random = new Random();
    protected String m_CaptCha = "ABCDEFGHTML123456789ftghyupxqc";
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Button1.Attributes.Add("onclick", "return OnSubmitClick();");
        this.btnClear.Attributes.Add("onclick", "return ClearAll();");
        
        if (!this.IsPostBack)
        {
            lblCaptcha.Visible = false;
            this.Session["CaptchaImageText"] = GenerateRandomCode();
            LoadLienHe();
            String strPage = "";
            try
            {
                strPage = Request.Params["Pro"].ToString().Trim();
            }
            catch
            {

            }
            lblContentTitle.Text = Constant.ContentTitle(strPage);
        }

    }

    public void RaiseCallbackEvent(String eventArgument)
    {
        returnValue = eventArgument;
    }

    private void LoadLienHe() {
        WebData db = new WebData();
        String strSql = "select top 1 lienhe from SysDamiWeb";
        DataTable tb = db.tbl_Categorys.GetDataTableCache("thongtinlienhe", Constant.time, strSql, CommandType.Text);
        String strLienhe = "";
        if (tb.Rows.Count > 0) {
            strLienhe = tb.Rows[0]["lienhe"].ToString().Trim();
        }
        lblLienhe.Text = strLienhe;
    }
    public String GetCallbackResult()
    {
        return returnValue;
    }

    private void LoadFooter()
    {
        using (DamiWebDataContext db = new DamiWebDataContext())
        {
            var query = from p in db.SysDamiWebs
                        select p;
            foreach (var c in query)
            {
              //  lblFooter.Text = c.footer;
            }
        }
    }

    private String GenerateRandomCode() {
        string s = "";
        int index1 = 0;
        int l = m_CaptCha.Length-1;
        for (int i = 0; i < 6; i++)
        {
            index1 = Convert.ToInt32(this.random.Next(l).ToString());
            s = s + m_CaptCha[index1].ToString();
        }
        return s;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        String strCode = Request.Form["txtCode"].ToString();
        String nguoigui = Request.Form["txtName"].ToString();
        String email = Request.Form["txtEmail"].ToString();
        String dienthoai = Request.Form["txtPhone"].ToString();
        String noidung = Request.Form["txtNoidung"].ToString();
        String tieude = Request.Form["txtTieude"].ToString();

        if (strCode.ToLower() != Session["CaptchaImageText"].ToString().ToLower())
        {
            lblCaptcha.Visible = true;
            this.Session["CaptchaImageText"] = GenerateRandomCode();
        }
        else
        {
            lblCaptcha.Visible = false;
            StringBuilder body = new StringBuilder();
            body.Append("Tiêu đề :" + tieude);
            body.AppendLine("Người gửi: " + nguoigui);
            body.AppendLine("Điện thoại: " + dienthoai);
            body.AppendLine("Email: " + email);
            body.AppendLine("Nội dung: " + noidung);
           // Sendmail(Constant.strEmail, email, body.ToString(), tieude);
          SendEmail m_mail = new SendEmail();
          Boolean bBool = m_mail.Sendmail(Constant.email, tieude, body.ToString(), false);
            this.Session["CaptchaImageText"] = GenerateRandomCode();
           if(bBool) 
               ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('Thông tin của bạn đã được gửi cho DaMi. Chúng tôi sẽ hồi âm sớm nhất. Cám ơn bạn!');", true);
        }
    }

    private Boolean Sendmail(String mailTo,String mailSend,String content,String strSubject) {
        Boolean bBool = false;
        try
        {
            MailMessage message = new MailMessage(new MailAddress(mailSend), new MailAddress(mailTo));
            message.Body = content.ToString();
            message.Subject = strSubject;
            SmtpClient client = new SmtpClient();
            client.Send(message);
            bBool = true;
        }
        catch{}
        return bBool;
    }
}